<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace web\api\controller;

use addons\video\model\VideoLike;
use addons\video\model\Video as VideoM;
use addons\video\model\VideoTags;
use addons\video\model\VideoActor;

/**
 * Description of Video
 * 视频
 * @author zmh
 */
class Video extends UserBase{

    /**
     * 热门搜索
     */
    public function hotSearchKey(){
        try{
            $videoM = new \addons\video\model\VideoSearch;
            $list = $videoM->where(['is_show'=>1])->column('hot_search');
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 发布视频
     */
    public function releaseVideo(){
        try{
            $VideoM = new VideoM;
            $data['user_id'] = $this->user_id;
            $data['video_title'] = $this->getParamVal('video_title');
            $data['video_img'] = $this->_post('video_img');
            $data['video_length'] = $this->_post('video_length');
            $data['video_path'] = $this->getParamVal('video_path');
            $data['tags'] = $this->_post('tags');
            $data['is_public'] = 1;
            $data['create_time'] = NOW_DATETIME;
            $data['update_time'] = NOW_DATETIME;
            $res = $VideoM->add($data);
            if($res) return $this->successJSON();
            else return $this->failJSON('上传视频失败');
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 视频分类列表
     */
    public function videoClassList(){
        try{
            $videoClassM = new \addons\video\model\VideoClass();
            $type = $this->_post('type')==1?0:1;
            if($type==1){
                $where = 'is_show=1 and video_type = 1';
            }else{
                $where = 'is_show=1 and video_type = 0';
            }
            $list = $videoClassM->getDataList($this->getPageIndex(),$this->getPageSize(),$where,'id,class_title', 'order_index desc');
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 获取关注演员列表
     */
    public function actorList(){
        try{
            $ids = (new \addons\member\model\MemberFocus)->where('user_id',$this->user_id)->column('to_user_id');
            $list = $this->userM->field('id user_id,username,head_img')->where(['is_actor'=>1,'id'=>['not in',$ids]])->limit(0,10)->order('sort asc')->select();
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


    /**
     * 视频列表
     */
    public function videoList(){
        try{
            $class_id = $this->_post('class_id');
            $user_id = $this->_post('user_id');
            $video_title = $this->_post('video_title');
            $is_free = $this->_post('is_free')==1?1:0;
            $filter = ' status=1 and is_public = 1 ';
            if($video_title != ''){
                $filter .= ' and video_title like \'%' . $video_title . '%\'';
            }
            $fields = 'a.id,a.user_id,a.video_title,a.is_free,a.video_img,a.video_view,tags,a.video_path,a.video_length,a.need_amount,a.praise_number,b.username,b.head_img,c.class_title';
            if($class_id != ''){
                $filter .= ' and a.class_id = '.$class_id;
            }
            if($user_id != ''){
                $filter .= ' and user_id = '.$user_id;
            }
            $filter .= ' and is_free='.$is_free;
            $videoM = new VideoM;
            $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, 'RAND() desc');
            // $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, 'id desc');
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 搜索视频
     */
    public function searchList(){
        $type = $this->_post('type')==1?1:2;
        $keyword = $this->_post('keyword');
        $is_hot = $this->_post('is_hot');
        $filter = ' status=1 and is_public = 1 ';
        if($type==1){
            $filter .= ' and a.video_type = 0 and video_title like \'%' . $keyword . '%\'';
        }else{
            $filter .= ' and a.video_type = 1 and video_title like \'%' . $keyword . '%\'';
        }
        if($is_hot!=''){
            $order = 'praise_number desc';
            // $filter .= ' and a.is_hot = 1 ';
        }else{
            $order = 'id desc';
        }
        $fields = 'a.id,a.user_id,a.video_title,a.is_free,a.video_img,a.video_view,tags,a.video_path,a.video_length,a.need_amount,a.praise_number,b.username,b.head_img,c.class_title';
        $videoM = new VideoM;
        $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, 'RAND() desc');
        // $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, 'id desc');
        $lists = [];
        if(empty($list)){
            $lists = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),'status=1 and is_public = 1 ', $fields, 'RAND() desc');
        }
        return $this->successJSON(['list'=>$list,'more'=>$lists]);
    }


    /**
     * 搜索视频
     */
    public function searchActor(){
        $filter = ' is_show = 1 ';
        $keyword = $this->_post('keyword');
        $fields = '*';
        $videoM = new VideoActor;
        $filter .= ' and actor_name like \'%' . $keyword . '%\'';
        // $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, 'RAND() desc');
        $list = $videoM->getDataList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, 'sort asc');
        $lists = [];
        if(empty($list)){
            $lists = $videoM->getDataList($this->getPageIndex(),$this->getPageSize(),'is_show = 1 ', $fields, 'sort asc');
        }
        return $this->successJSON(['list'=>$list,'more'=>$lists]);
    }


    /**
     * 关注用户
     */
    public function userFocus(){
        try{
            $focusM = new \addons\member\model\MemberFocus();
            $to_user_id = $this->getParamVal('to_user_id');
            if($to_user_id == $this->user_id) return $this->failJSON('不可关注自己');
            $re = $focusM->getFocus($this->user_id, $to_user_id);
            if($re < 1) return $this->failJSON('请勿重复提交');
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }
    


    /**
     * 关注视频列表
     */
    public function focusList(){
        $m = new \addons\member\model\MemberFocus();
        $type = $this->_post('type')==1?0:1;
        $ids = $m->field('group_concat(to_user_id) ids')->where('user_id',$this->user_id)->find();
        if(!$ids['ids']) return $this->successJSON([]);
        $fields = 'a.id,a.user_id,a.video_title,a.video_img,a.video_path,a.praise_number,a.com_number,a.video_type,a.create_time,b.username,b.head_img';
        $filter = ' status=1 and user_id in('.$ids['ids'].') and a.video_type='.$type;
        $videoM = new VideoM;
        $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, 'a.id desc');
        return $this->successJSON($list);
    }

    /**
     * 我的关注和我的粉丝列表
     */
    public function attentionList(){
        try{
            $type = $this->_post('type');
            $user_id = $this->_post('user_id');
            if(!in_array($type, [1,2])){
                return $this->failJSON('参数错误');
            }
            if($user_id == ''){
                $user_id = $this->user_id;
            }
            if($type == 1){
                $filter = ' user_id = '. $user_id;
                $fields = ' a.user_id,a.to_user_id,a.is_focus,b.username as username,b.head_img,b.intro';
                $order = ' a.id desc ';
                $list = (new \addons\member\model\MemberFocus())->getUserList($this->getPageIndex(), $this->getPageSize(), $filter, $fields, $order);
            }else{
                $filter = ' to_user_id = '. $user_id;
                $fields = ' a.user_id,a.to_user_id,a.is_focus,b.username as to_username,b.head_img,b.intro';
                $order = ' a.id desc ';
                $list = (new \addons\member\model\MemberFocus())->getToUserList($this->getPageIndex(), $this->getPageSize(), $filter, $fields, $order);
            }
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 我的视频列表
     */
    public function myVideoList(){
        $fields = 'a.id,a.video_title,a.video_img,a.video_path,a.praise_number,a.status,a.com_number,b.username,b.head_img,c.class_title';
        $filter = ' user_id = '.$this->user_id;
        $videoM = new VideoM;
        $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, 'a.id desc');
        return $this->successJSON($list);
    }

    /**
     * 视频详情
     */
    public function videoView(){
        $map['id'] = $this->_post('v_id');
        $map['video_type'] = 0;
        $video = new VideoM;
        $info = $video->field('id,user_id,video_title,video_img,video_length,video_path,video_length,video_size,praise_number,tags,com_number,create_time,is_free,need_amount')->where($map)->find();
        if($info){
            $userdata = $this->userM->getDetail($info['user_id'],'username,head_img');
            $info['username'] = $userdata['username'];
            $info['head_img'] = $userdata['head_img'];
            $focusM = new \addons\member\model\MemberFocus();
            $is_focus = $focusM->where(['user_id' => $this->user_id, 'to_user_id' => $info['user_id']])->find();
            //是否关注了此人
            $info['is_focus'] = 0;
            if($is_focus){
                $info['is_focus'] = 1;
            }
            $info['is_pay'] = 0;
            if($info['need_amount']>0){
                $ex = (new \addons\video\model\VideoPay)->where(['user_id'=>$this->user_id,'video_id'=>$map['id']])->find();
                if(strtotime($ex['end_time'])>time()){
                    $info['is_pay'] = 1;
                }
            }else{
                $info['need_amount'] = 0;
            }
            //是否点过赞
            $likeM = new \addons\video\model\VideoLike();
            $is_like = $likeM->where(['video_id' => $map['id'], 'user_id' => $this->user_id])->find();
            $info['is_like'] = 0;
            if($is_like){
                $info['is_like'] = 1;
            }
            $data = (new VideoTags)->where('is_on',1)->column('id,tags_name');
            foreach (explode(',', $info['tags']) as $key => $val) {
                if(isset($data[$val])) {
                    $tmp['id'] = $val;
                    $tmp['name'] = $data[$val];
                    $info['tags_name'][] = $tmp;
                }
            }
            $video->where($map)->setInc('video_view');
        }
        return $this->successJSON($info);
    }


     /**
     * 影片详情
     */
    public function filmDetail(){
        $map['id'] = $this->_post('v_id');
        $map['video_type'] = 1;
        $video = new VideoM;
        $info = $video->field('id,user_id,video_title,video_img,video_length,video_path,video_size,praise_number,tags,com_number,create_time,is_free,need_amount')->where($map)->find();
        if($info){
            $userdata = $this->userM->getDetail($info['user_id'],'username,head_img');
            $info['username'] = $userdata['username'];
            $info['head_img'] = $userdata['head_img'];
            $focusM = new \addons\member\model\MemberFocus();
            $is_focus = $focusM->where(['user_id' => $this->user_id, 'to_user_id' => $info['user_id']])->find();
            //是否关注了此人
            $info['is_focus'] = 0;
            if($is_focus){
                $info['is_focus'] = 1;
            }
            $info['is_pay'] = 0;
            if($info['need_amount']>0){
                $ex = (new \addons\video\model\VideoPay)->where(['user_id'=>$this->user_id,'video_id'=>$map['id']])->find();
                if(strtotime($ex['end_time'])>time()){
                    $info['is_pay'] = 1;
                }
            }else{
                $info['need_amount'] = 0;
            }
            //是否点过赞
            $likeM = new \addons\video\model\VideoLike();
            $is_like = $likeM->where(['video_id' => $map['id'], 'user_id' => $this->user_id])->find();
            $info['is_like'] = 0;
            if($is_like){
                $info['is_like'] = 1;
            }
            $data = (new VideoTags)->where('is_on',1)->column('id,tags_name');
            foreach (explode(',', $info['tags']) as $key => $val) {
                if(isset($data[$val])) {
                    $tmp['id'] = $val;
                    $tmp['name'] = $data[$val];
                    $info['tags_name'][] = $tmp;
                }
            }
            $video->where($map)->setInc('video_view');
            $info['more'] = $video->field('v.id,user_id,video_title,video_img,video_length,video_path,praise_number,v.create_time,username,head_img')->alias('v')->join('member_account m','m.id=v.user_id and m.is_actor = 1')->where(['video_type'=>1,'user_id'=>$info['user_id'],'v.id'=>['<>',$map['id']]])->limit(0,10)->select();
        }
        return $this->successJSON($info);
    }


    /**
     * 推荐视频
     */
    public function recommendList(){
        $map['v.id'] = ['<>',$this->_post('v_id')];
        $map['video_type'] = 1;
        $video = new VideoM;
        $list = $video->field('v.id,user_id,video_title,video_img,video_length,video_path,praise_number,v.create_time,username,head_img')->alias('v')->join('member_account m','m.id=v.user_id and m.is_actor = 1')->where($map)->limit(0,10)->select();
        return $this->successJSON($list);
    }

    /**
     * 演员详情
     */
    public function actorDetail(){
        try{
            $video = new VideoM;
            $map['user_id'] = $this->getParamVal('actor_id');
            $info = $this->userM->getDetail($map['user_id'],'username,head_img,intro');
            $MemberFocus = new \addons\member\model\MemberFocus;
            $info['focus_num'] = $MemberFocus->where(['to_user_id'=>$map['user_id']])->count();
            $info['like_num'] = $video->where(['user_id'=>$map['user_id']])->sum('praise_number');
            $is_focus = $MemberFocus->where(['user_id'=>$this->user_id,'to_user_id'=>$map['user_id']])->value('id');
            $info['is_focus'] = $is_focus>0?1:0;
            $fields = 'id,video_title,video_img,video_length,video_path,praise_number';

            $small_page = $this->_post('small_page');
            if($small_page == '' || $small_page == null){
                $small_page = 0;
            }
            $small_rows = $this->_post('small_rows');
            if($small_rows == '' || $small_rows == null){
                $small_rows = 10;
            }
            $film_page = $this->_post('film_page');
            if($film_page == '' || $film_page == null){
                $film_page = 0;
            }
            $film_rows = $this->_post('film_rows');
            if($film_rows == '' || $film_rows == null){
                $film_rows = 10;
            }
            $info['small_video'] = $video->field($fields)->where(['user_id'=>$map['user_id'],'video_type'=>0])->limit($small_page*$small_rows,$small_rows)->select();
            $info['film'] = $video->field($fields)->where(['user_id'=>$map['user_id'],'video_type'=>1])->limit($film_page*$film_rows,$film_rows)->select();
            return $this->successJSON($info);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 喜欢视频 视频点赞
     */
    public function likeVideo(){
        $videoM = new VideoM;
        $likeM = new \addons\video\model\VideoLike();
        $video_id = $this->_post('v_id');
        if($video_id<=0) return $this->failJSON('参数错误');
        $ex = $likeM->where(['video_id'=>$video_id,'user_id'=>$this->user_id])->find();
        $videoM->startTrans();
        if($ex){
            $info = $likeM->likeVideo($this->user_id,$video_id);
            if(!$info) {
                $videoM->rollback();
                return $this->failJSON('操作失败');
            }
            $re_v = $videoM->updatePraiseCom($video_id, 1, 2);
            if(!$re_v) {
                $videoM->rollback();
                return $this->failJSON('操作失败');
            }
        }else{
            $info = $likeM->likeVideo($this->user_id,$video_id,1);
            if(!$info) {
                $videoM->rollback();
                return $this->failJSON('操作失败');
            }
            $re_v = $videoM->updatePraiseCom($video_id, 1, 1);
            if(!$re_v) {
                $videoM->rollback();
                return $this->failJSON('操作失败');
            }
        }
        $videoM->commit();
        return $this->successJSON();
    }


    /**
     * 小视频
     */
    public function smallVideoList(){
        try{
            $class_id = $this->_post('class_id');
            $filter = ' status=1 and is_public=1 and a.video_type=0 ';
            $fields = 'a.id,a.user_id,a.video_title,a.is_free,a.video_img,a.video_view,a.video_path,a.video_length,need_amount,a.praise_number,b.username,b.head_img,c.class_title';
            $order = 'rand() desc';
            if($class_id != ''){
                $filter .= ' and a.class_id = '.$class_id;
            }
            $videoM = new VideoM;
            // $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, 'RAND() desc');
            $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, $order);
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


    /**
     * 影片推荐
     */
    public function filmVideoList(){
        try{
            $videoClassM = new \addons\video\model\VideoClass();
            $list = $videoClassM->getDataList($this->getPageIndex(),4,'is_show=1 and video_type = 1 ','id,class_title', 'order_index desc');
            $fields = 'a.id,a.user_id,a.video_title,a.is_free,a.video_img,a.video_view,a.video_path,a.video_length,need_amount,a.praise_number,b.username,b.head_img,c.class_title';
            $videoM = new VideoM;
            foreach ($list as $k => &$v) {
                $filter = ' status=1 and is_public=1 and a.video_type=1 and a.class_id = '.$v['id'];
                $v['list'] = $videoM->getVideoList($this->getPageIndex(),10,$filter, $fields, 'rand() desc');
            }
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 影片钻石
     */
    public function filmAmountList(){
        try{
            $filter = ' status=1 and is_public=1 and a.video_type=1 and need_amount>0';
            $fields = 'a.id,a.user_id,a.video_title,a.is_free,a.video_img,a.video_view,a.video_path,a.video_length,need_amount,a.praise_number,b.username,b.head_img,c.class_title';
            $order = 'rand() desc';
            $videoM = new VideoM;
            // $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, 'RAND() desc');
            $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, $order);
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


    /**
     * 影片推荐
     */
    public function filmClassList(){
        try{
            $class_id = $this->getParamVal('class_id');
            $filter = ' status=1 and is_public=1 and a.video_type=1 ';
            $fields = 'a.id,a.user_id,a.video_title,a.is_free,a.video_img,a.video_view,a.video_path,a.video_length,need_amount,a.praise_number,b.username,b.head_img,c.class_title';
            $filter .= ' and a.class_id = '.$class_id;
            $order = 'rand() desc';
            $videoM = new VideoM;
            // $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, 'RAND() desc');
            $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, $order);
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


    /**
     * 视频上下
     */
    public function videoNext(){
        $user_id = $this->_post('user_id')??0;
        $type = $this->_post('type')??0;
        $video_id = $this->_post('v_id')??0;
        $is_rand = $this->_post('is_rand')==1?1:0;
        if($is_rand==1){
            $order_str = 'RAND() desc';
        }else{
            $order_str = 'id asc';
        }
        $videoM = new VideoM;

        $filter = ' and status=1';
        if($video_id > 0){
            $filter .= ' and id >= '. $video_id;
        }

        if($user_id > 0){
            $filter .= ' and user_id = '. $user_id;
        }else{
            if($type == 1){
                $filter .= ' and user_id = '. $this->user_id;
            }
        }
        $list = $videoM->getDataList($this->getPageIndex(),$this->getPageSize(), $filter, '*', $order_str);
        if($list){
            foreach ($list as &$value){
                $user_data = $this->userM->getDetail($value['user_id'], 'username,head_img');
                $value['username'] = $user_data['username'];
                $value['head_img'] = $user_data['head_img'];
                $focusM = new \addons\member\model\MemberFocus();
                $is_focus = $focusM->where(['user_id' => $this->user_id, 'to_user_id' => $value['user_id']])->find();
                //是否关注了此人
                $value['is_focus'] = 0;
                if($is_focus){
                    $value['is_focus'] = 1;
                }
                //是否点过赞
                $likeM = new \addons\video\model\VideoLike();
                $is_like = $likeM->where(['video_id' => $value['id'], 'user_id' => $this->user_id])->find();
                $value['is_like'] = 0;
                if($is_like){
                    $value['is_like'] = 1;
                }
                $value['goods_list'] = (new \addons\shop\model\Goods())->getDetail($value['goods_id'], 'id,goods_name,sales_sum,images,price');
            }
        }
        return $this->successJSON($list);
    }

    /**
     * 我喜欢的视频上下
     */
    public function likeVideoNext(){
        $user_id = $this->_post('user_id')??0;
        if($user_id <= 0){
            $user_id = $this->user_id;
        }
        $video_id = $this->_post('v_id');
        $likeM = new \addons\video\model\VideoLike();
        $video_arr = $likeM->where(['user_id' => $user_id])->column('video_id');
        $list = [];
        if($video_arr){
            $videoM = new VideoM;
            $video_list = implode(',',$video_arr);
            $filter = ' and status=1 and id >= '.$video_id. ' and id in('.$video_list.')';
            $list = $videoM->getDataList(1,10, $filter, '*', 'id asc');
            if($list){
                foreach ($list as &$value){
                    $user_data = $this->userM->getDetail($value['user_id'], 'username,head_img');
                    $value['username'] = $user_data['username'];
                    $value['head_img'] = $user_data['head_img'];
                    $focusM = new \addons\member\model\MemberFocus();
                    $is_focus = $focusM->where(['user_id' => $this->user_id, 'to_user_id' => $value['user_id']])->find();
                    //是否关注了此人
                    $value['is_focus'] = 0;
                    if($is_focus){
                        $value['is_focus'] = 1;
                    }
                    //是否点过赞
                    $is_like = $likeM->where(['video_id' => $value['id'], 'user_id' => $this->user_id])->find();
                    $value['is_like'] = 0;
                    if($is_like){
                        $value['is_like'] = 1;
                    }
                    // $value['goods_list'] = (new \addons\shop\model\Goods())->getDetail($value['goods_id'], 'id,goods_name,sales_sum,images,price');
                }
            }
        }
        return $this->successJSON($list);
    }


    /**
     * 影片  演员列表
     */
    public function allActorList(){
        try{
            $m = new \addons\member\model\MemberClass();
            $page = $this->getPageIndex()-1>0?$this->getPageIndex()-1:0;
            $rows = 5;
            $list = $m->where('is_show=1')->limit($page*$rows,$rows)->order('order_index desc')->select();
            foreach ($list as $k => &$v) {
                $v['list'] = $this->userM->field('id,username,head_img')->where(['class_id'=>$v['id']])->select();
            }
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }



    /**
     * 视频删除
     */
    public function videoDelete(){
        try{
            $VideoM = new VideoM;
            $map['id'] = $this->getParamVal('video_id');
            $user_id = $VideoM->where($map)->value('user_id');
            if(!$user_id) return $this->failJSON('该视频已删除');
            if($user_id!=$this->user_id) return $this->failJSON('您不能删除不是自己发布的视频');
            $map['user_id'] = $user_id;
            $res = $VideoM->where($map)->delete();
            if($res) return $this->successJSON();
            else return $this->failJSON('视频删除失败');
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


     /**
     * 礼物列表
     */
    public function gitfList(){
        $gift = new \addons\video\model\GiftModel();
        $list = $gift->where('status',1)->order('gift_amount asc')->select();
        return $this->successJSON($list);
    }


    /**
     * 礼物赠送
     */
    public function sendGift(){
        try{
            $gift_id = $this->getParamVal('gift_id',true,'int');
            $anchor_id = $this->getParamVal('anchor_id',true,'int');
            $click_count = $this->getParamVal('num',true,'int');
            $res = (new \addons\video\model\GiftModel())->sendGift($this->user_id, $gift_id, $anchor_id, $click_count);
            if($res['success']){
                return $this->successJSON();
            }else{
                return $this->failJSON($res['message']);
            }
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 我的礼物列表 打赏记录
     */
    public function rewardList(){
        $m = new \addons\video\model\GiftLog();
        $data['times'] = $m->where('user_id',$this->user_id)->count();
        $data['total'] = $m->alias('l')->where('l.user_id',$this->user_id)->join('live_gift g','g.id=l.gift_id','left')->sum('gift_amount*l.count');
        $page = $this->getPageIndex()-1>0?$this->getPageIndex()-1:0;
        $rows = $this->getPageSize()>0?$this->getPageSize():10;
        $data['list'] = $m->alias('l')->field('m.id id,m.username,l.create_at,m.phone,l.create_at,m.head_img,g.gift_name,g.gift_amount,g.gift_url')->join('member_account m','l.anchor_id=m.id','left')->join('live_gift g','g.id=l.gift_id','left')->where('l.user_id',$this->user_id)->limit($page*$rows,$rows)->select();
        $this->successJSON($data);
    }

    /**
     * 我喜欢的视频列表
     */
    public function myLikeVideoList(){
        try{
            $type = $this->_post('type')==1?0:1;
            $filter = ' and a.user_id = '.$this->user_id;
            if($type==0){
                $filter .= ' and video_type = 0 ';
            }else{
                $filter .= ' and video_type = 1 ';
            }
            $fields = 'v.id,v.video_img,video_title,praise_number,video_length,m.username,m.head_img';
            $m = new VideoLike;
            $list = $m->getUserLikeList($this->getPageIndex(),$this->getPageSize(), $filter, $fields, 'a.id desc');
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 我支付的视频列表
     */
    public function myPayVideoList(){
        try{
            $type = $this->_post('type')==1?0:1;
            $filter = ' and a.user_id = '.$this->user_id;
            if($type==1){
                $filter .= ' and video_type = 0 ';
            }else{
                $filter .= ' and video_type = 1 ';
            }
            $fields = 'v.id,v.video_img,video_title,praise_number,video_length,m.username,m.head_img';
            $m = new \addons\video\model\VideoPay;
            $list = $m->getUserLikeList($this->getPageIndex(),$this->getPageSize(), $filter, $fields, 'a.id desc');
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 标签收藏
     */
    public function tagsLike(){
        try{
            $likeM = new \addons\video\model\VideoTagsLike;
            $tags_id = $this->getParamVal('tags_id',true,'int');
            $res = $likeM->likeTags($this->user_id,$tags_id);
            if($res){
                return $this->successJSON();
            }else{
                return $this->failJSON('操作失败');
            }
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 收藏信息
     */
    public function collectionInfo(){
        try{
            $likeM = new VideoLike;
            $info['small'] = $likeM->alias('a')->join('video v','v.id=a.video_id')->where('video_type=0 and a.user_id='.$this->user_id)->count();
            $info['film'] = $likeM->alias('a')->join('video v','v.id=a.video_id')->where('video_type=1 and a.user_id='.$this->user_id)->count();
            $info['tags'] = (new \addons\video\model\VideoTagsLike)->where('user_id='.$this->user_id)->count();
            $info['abount'] = (new \addons\about\model\AboutLike)->where('user_id='.$this->user_id)->count();
            return $this->successJSON($info);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 解锁信息
     */
    public function unlockInfo(){
        try{
            $payM = new \addons\video\model\VideoPay;
            $info['small'] = $payM->alias('a')->join('video v','v.id=a.video_id')->where('video_type=0 and a.user_id='.$this->user_id)->count();
            $info['film'] = $payM->alias('a')->join('video v','v.id=a.video_id')->where('video_type=1 and a.user_id='.$this->user_id)->count();
            $info['abount'] = (new \addons\about\model\AboutOrder)->where('status = 1 and user_id='.$this->user_id)->count();
            return $this->successJSON($info);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 标签信息
     */
    public function tagsInfo(){
        try{
            $tags_id = $this->getParamVal('tags_id',true,'int');
            $tagsM = new \addons\video\model\VideoTags;
            $videoM = new \addons\video\model\Video;
            $likeM = new \addons\video\model\VideoTagsLike;
            $info = $tagsM->field('id,tags_name,intro')->where(['id'=>$tags_id,'is_on'=>1])->find();
            $info['count'] = $likeM->where('tags_id='.$tags_id)->count();
            $info['img'] = $likeM->alias('l')->where(['tags_id'=>$tags_id,'head_img'=>['neq','']])->join('member_account m','m.id=l.user_id')->column('head_img');
            $info['sum'] = $videoM->where(['tags'=>['like','%'.$tags_id.'%']])->sum('video_view');
            $info['is_like'] = 0;
            $ex = $likeM->where(['user_id'=>$this->user_id,'tags_id'=>$tags_id])->value('id');
            if($ex) $info['is_like'] = 1;
            return $this->successJSON($info);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 标签信息
     */
    public function likeTagsList(){
        try{
            $tagsM = new \addons\video\model\VideoTags;
            $videoM = new \addons\video\model\Video;
            $likeM = new \addons\video\model\VideoTagsLike;
            $page = $this->getPageIndex()-1>0?$this->getPageIndex()-1:0;
            $rows = $this->getPageSize()>0?$this->getPageSize():10;
            $list = $likeM->alias('l')->join('video_tags t','t.id=l.tags_id')->field('t.id,tags_name,intro')->where(['user_id'=>$this->user_id,'is_on'=>1])->limit($page*$rows,$rows)->select();
            foreach ($list as $k => &$v) {
                $v['count'] = $likeM->where('tags_id='.$v['id'])->count();
                $v['img'] = $likeM->alias('l')->where(['tags_id'=>$v['id'],'head_img'=>['neq','']])->join('member_account m','m.id=l.user_id')->column('head_img');
                $v['sum'] = $videoM->where(['tags'=>['like','%'.$v['id'].'%']])->sum('video_view');
            }
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


    /**
     * 标签视频
     */
    public function tagsVideoList(){
        try{
            $tags_id = $this->getParamVal('tags_id',true,'int');
            $type = $this->_post('type')==1?0:1;
            $is_new = $this->_post('is_new')==1?0:1;
            if($is_new==1){
                $order = 'id desc';
            }else{
                $order = 'praise_number desc';
            }
            $filter = ' status=1 and is_public = 1 and a.video_type='.$type;
            $filter .= ' and tags like \'%' . $tags_id . '%\'';
            $fields = 'a.id,a.user_id,a.video_title,a.is_free,a.video_img,a.video_view,tags,a.video_path,a.video_length,a.praise_number,b.username,b.head_img,c.class_title';
            $videoM = new VideoM;
            // $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, 'RAND() desc');
            $list = $videoM->getVideoList($this->getPageIndex(),$this->getPageSize(),$filter, $fields, $order);
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }
}
